<?php
/**
 * Created by PhpStorm.
 * User: alex
 * Date: 2018/11/9
 * Time: 下午2:39
 */

namespace ApiBundle\Controller;


use BaseBundle\Services\ArticleClassService;
use BaseBundle\Services\ArticleService;
use Symfony\Component\DependencyInjection\ContainerInterface;

class ArticleController extends ApiController
{
    /**
     * @var ArticleService
     */
    private $articleService;

    /**
     * @var ArticleClassService
     */
    private $articleClassService;

    public function setContainer(ContainerInterface $container = null)
    {
        parent::setContainer($container);
        if (!empty($container)){
            $this->articleService = new ArticleService($container);
            $this->articleClassService = new ArticleClassService($container);
        }
    }

    /**
     * @return bool|\Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     */
    public function articlesByQueryAction()
    {
        $r = $this->inlet($this->getParameter('return_data'),false);
        if($r !== true){
            return $r;
        }
        $key_word = self::$request->query->get('key_word','');
        $article_class_id = self::$request->query->get('article_class_id','');

        //article_class_id查询添加次数
        if (!empty($article_class_id) && $article_class_id != -1){
            $article_class_info = $this->articleClassService->getInfo($article_class_id);
            if (empty($article_class_info)){
                return $this->response($this->getParameter('system_busy_msg'));
            }

            //验证分类并添加次数
            $this->articleClassService->updateOther(
                ['view_times'=>$article_class_info['view_times']+1],['id'=>$article_class_id]
            );
        }

        $data = $this->articleService->getList(
            ['join_article_class' => 1, 'select_article_class_msg' => 1, 'where_key_word' => $key_word,'article_class_id' => $article_class_id ]
        );

        return $this->response($this->getParameter('success_find_date_msg'),0,$data);
    }

    /**
     * @return bool|\Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     */
    public function articlesByArticleClassAction($article_class_id)
    {
        $r = $this->inlet($this->getParameter('return_data'),false);
        if($r !== true){
            return $r;
        }

        $article_class_info = $this->articleClassService->getInfo($article_class_id);
        if (empty($article_class_id)){
            return $this->response($this->getParameter('parameter_error_msg'));
        }

        if (empty($article_class_info)){
            return $this->response($this->getParameter('system_busy_msg'));
        }

        $data = $this->articleService->getArticleByArticleClass(
            ['join_article_class' => 1, 'select_article_class_msg' => 1 ],
            $article_class_id
        );

        //验证分类并添加次数
        $this->articleClassService->updateOther(
            ['view_times'=>$article_class_info['view_times']+1],['id'=>$article_class_id]
        );
        return $this->response($this->getParameter('success_find_date_msg'),0,$data);
    }

    /**
     * 推荐文章
     *
     * @return bool|\Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     */
    public function recommendArticlesAction()
    {
        $r = $this->inlet($this->getParameter('return_data'),false);
        if($r !== true){
            return $r;
        }

        $data = $this->articleService->getRecommendArticles([]);

        return $this->response($this->getParameter('success_find_date_msg'),0,$data);
    }

    /**
     * 文章详情
     *
     * @param $article_id
     * @return array|bool|\Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     */
    public function articlesInfoAction($article_id)
    {
        $r = $this->inlet($this->getParameter('return_data'),false);
        if($r !== true){
            return $r;
        }

        if (empty($article_id)){
            $this->response($this->getParameter('parameter_error_msg'));
        }


        $data['info'] = $this->articleService->getInfo(
            $article_id,
            ['join_article_class' => 1, 'select_article_class_msg' => 1 ],
            1
        );

        $this->articleService->updateOther(['view_times'=>$data['info']['view_times']+1],['id'=>$article_id]);

        return $this->response($this->getParameter('success_find_date_msg'),0,$data);
    }
}